package com.ihuipao.game.Objects;

import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;

import com.blankj.utilcode.util.CacheDiskUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.nanchen.compresshelper.CompressHelper;

import java.io.File;

public class Photo {
    private String path;
    private int progress;
    private Exif exif;
    private Bitmap bitmap;
    public Photo(String path){
        this.path = path;
    }

    public Exif getExif(){
        if(exif==null){
            exif = new Exif(this.path);
        }
        return exif;
    }

    public String getSize(){
        return FileUtils.getSize(this.path);
    }

    public long getLength(){
        return FileUtils.getFileLength(this.path);
    }

    public Bitmap getBitmap(){
        if(this.bitmap==null){
            this.bitmap = ImageUtils.getBitmap(path);
        }

        return this.bitmap;
    }

    public int getWidth(){
        return getBitmap().getWidth();
    }

    public int getHeight(){
        return getBitmap().getHeight();
    }

    public String getMd5(){
        return FileUtils.getFileMD5ToString(this.path).toLowerCase();
    }

    public boolean isUpload(String type){
        String key = String.format("uploaded:%s:%s", type, path);
        String uploadtime = CacheDiskUtils.getInstance().getString(key);
        if(uploadtime==null || uploadtime==""){
            return false;
        }else{
            return true;
        }
    }

    public void setUpload(String type){
        String key = String.format("uploaded:%s:%s", type, path);
        CacheDiskUtils.getInstance().put(key, TimeUtils.getNowString());
    }

    public void setProgress(int progress){
        this.progress = progress;
    }

    public int getProgress(){
        return this.progress;
    }

    public String getPath(){
        return this.path;
    }

    public File getFile(){
        try{
            File file = new File(path);
            return file;
        }catch (Exception ex){
            return null;
        }
    }

    public File compress(Context context){
        try {
            File file = new CompressHelper.Builder(context)
                    .setQuality(100)
                    .setMaxWidth(800)
                    .setMaxHeight(800)
                    .setCompressFormat(Bitmap.CompressFormat.JPEG)
                    .build()
                    .compressToFile(getFile());
            return file;
        }catch (Exception exception){

        }
        return null;
    }

    public void upload(){

    }

}
